package trees;

public class TreeNode {
    public int val;
    public TreeNode left, right;

    // 初始化节点
    public TreeNode(int val) {
        this.val = val;
        left = null;
        right = null;
    }

    // 先序遍历-根左右
    public void preTraverse(TreeNode treeNode) {
        if (treeNode == null) {
            return;
        }

        System.out.print(treeNode.val + " ");
        preTraverse(treeNode.left);
        preTraverse(treeNode.right);
    }

    // 中序遍历-左根右
    public void middleTraverse(TreeNode treeNode) {
        if (treeNode == null) {
            return;
        }

        middleTraverse(treeNode.left);
        System.out.print(treeNode.val + " ");
        middleTraverse(treeNode.right);
    }

    // 后序遍历-左右根
    public void lastTraverse(TreeNode treeNode) {
        if (treeNode == null) {
            return;
        }

        lastTraverse(treeNode.left);
        lastTraverse(treeNode.right);
        System.out.print(treeNode.val + " ");
    }

    public static TreeNode buildTree(int firstVal) {
        TreeNode treeNode = new TreeNode(firstVal);
        treeNode.left = new TreeNode(firstVal + 1);
        treeNode.right = new TreeNode(firstVal + 2);

        treeNode.left.left = new TreeNode(firstVal + 3);
        treeNode.left.right = new TreeNode(firstVal + 4);

        treeNode.right.left = new TreeNode(firstVal + 5);
        treeNode.right.right = new TreeNode(firstVal + 6);

        return treeNode;
    }
}
